using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.IO;
using Vit.Web;
using Vit.Web.Services;
using Vit.CMS.Data;

namespace Vit.CMS.Controllers
{
    public class PageController : Controller
    {
        private Vit.Web.Models.WebSite site;

        public PageController(Vit.Web.Models.WebSite site)
        {
            this.site = site;
        }

        public ActionResult Index(string id)
        {
            DirectoryInfo dir = new DirectoryInfo(Path.Combine(site.PhysicalRootPath, "Pages"));
            if (dir.Exists)
            {
                ViewData.Model = dir;
            }
            return PartialView();
        }

        public ActionResult Edit(string id)
        {
            FileInfo file = new FileInfo(id.PageFile());
            string path = file.FullName.Replace(".aspx", ".design.aspx");
            if (file.Exists)
                file.CopyTo(path, true);
            PageInfo pageInfo = new PageInfo();
            pageInfo.FilePath = path;
            pageInfo.Deserialize(null);
            ViewData.Model = pageInfo;
            return PartialView();
        }

        [HttpPost]
        public ActionResult Edit(PageInfo pageInfo)
        {
            if (!ModelState.IsValid)
                return PartialView(pageInfo);
            return PartialView();
        }
    }
}
